<template>
  <div class="customerStatistics">
    <div class="title">客户群监测<span class="icon">指标说明</span>
    </div>
    <ul class="dataScreening">
      <li>
        <div class="li-title">可监控群总数
          <el-tooltip class="item" effect="dark" content="可添加监控群的总个数" placement="top">
            <i class="el-icon-question"></i>
          </el-tooltip>
        </div>
        <div class="li-number">0</div>
      </li>
      <li>
        <div class="li-title">已监控群数
          <el-tooltip class="item" effect="dark" content="已添加监控的群总数" placement="top">
            <i class="el-icon-question"></i>
          </el-tooltip>
        </div>
        <div class="li-number">0</div>
      </li>
      <li>
        <div class="li-title">剩余可监控群数
          <el-tooltip class="item" effect="dark" content="新增群主是企微号的群聊个数" placement="top">
            <i class="el-icon-question"></i>
          </el-tooltip>
        </div>
        <div class="li-number">0</div>
      </li>
    </ul>
    <div class="btn-group">
      <el-button type="primary" plain icon="el-icon-plus" @click="getAddUserGroup"> 需监测的客户群</el-button>
      <el-button plain icon="el-icon-setting" @click="getAddRules"> 自动添加规则</el-button>
      <el-button plain icon="el-icon-stopwatch" @click="addRecord"> 添加记录</el-button>
      <el-button type="primary" icon="el-icon-delete"  plain> 批量移除群</el-button>
    </div>
    <div class="formGroup">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="130px" class="">
        <el-form-item prop="userName2">
          <el-select v-model="queryParams.userName2" size="small" style="width: 150px" filterable placeholder="全部账号" @change="handleQuery">
            <el-option
              v-for="item in userList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <div class="search" >
            <el-input v-model="queryParams.userName1" size="small" placeholder="搜索群名称" clearable style="width: 150px"/>
            <el-button icon="el-icon-search" size="small" @click="handleQuery"></el-button>
          </div>
        </el-form-item>
        <el-form-item>
          <div class="search" >
            <el-input v-model="queryParams.userName1" size="small" placeholder="搜索群主" clearable style="width: 150px"/>
            <el-button icon="el-icon-search" size="small" @click="handleQuery"></el-button>
          </div>
        </el-form-item>
        <el-form-item>
          <MultipleSelect size="small" placeholder='选择标签'  class="select-right" :receiverNameOpt='lableOpt'></MultipleSelect>
        </el-form-item>
        <el-form-item prop="userName2">
          <el-select v-model="queryParams.userName2" size="small" filterable placeholder="全部分组" @change="handleQuery">
            <el-option
              v-for="item in userList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="tag">
         <minOrMaxNumber  :placeholder="`群人数`" @getNumber="getNumber"></minOrMaxNumber>
        </el-form-item>
        <el-form-item prop="userName2">
          <div class="time-big">
            <ul class="timeType">
              <li :class="selectedRange=='today'?`active`:``" @click="getTime('today')">今天</li>
              <li :class="selectedRange=='yesterday'?`active`:``" @click="getTime('yesterday')">昨天</li>
              <li :class="selectedRange=='last7Days'?`active`:``" @click="getTime('last7Days')">近7天</li>
              <li :class="selectedRange=='last15Days'?`active`:``" @click="getTime('last15Days')">近15天</li>
              <li :class="selectedRange=='last30Days'?`active`:``" @click="getTime('last30Days')">近30天</li>
            </ul>
            <el-date-picker
              size="small"
              v-model="timeRange"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </div>

        </el-form-item>
        <el-button  plain size="small">重置</el-button>
      </el-form>
    </div>
    <div class="tableAnalysis">
      <div class="center-title"><span></span>分日数据统计</div>
      <el-button plain class="exportData">导出数据</el-button>
      <el-table
        :data="tableDataList"
        ref="customerMgtList"
        @sort-change="handleSortChange"
      >
        <el-table-column
          :label="item.label"
          align="center"
          v-for="(item, index) in tableColumns"
          :key="index"
          :prop="item.key"
          :min-width="item.minWidth ? item.minWidth : ``"
          :fixed="item.fixed ? item.fixed : false"
          :show-overflow-tooltip="true"
          :sortable="item.sortable?`custom`:``"
        >
          <!--用于表格自定义-->
          <template slot="header" v-if="item.showHeaderTip">
            <div>
              {{item.label}}
              <el-tooltip effect="dark" :content="item.showHeaderTip" placement="top">
                <span><i class="el-icon-question"></i></span>
              </el-tooltip>
            </div>
          </template>
          <template slot-scope="scope">
            <div>{{ scope.row[item.key] }}</div>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
<!--     需添加的客户群-->
    <addUserGroup v-if="showAddUserGroup" :visableIf.sync="showAddUserGroup"></addUserGroup>
<!--    添加自动规则-->
    <addRules v-if="showAddRules" :visableIf.sync="showAddRules"></addRules>
<!--    添加记录-->
    <addRecord v-if="showAddRecord" :visableIf.sync="showAddRecord"></addRecord>
  </div>
</template>
<script>
import {listUser} from "@/api/system/user";
import {getTime} from "@/utils";
import MultipleSelect from '@/views/components/multipleSelect.vue'

export default {
  name: "customerStatistics",
  components: {
    MultipleSelect,
    'minOrMaxNumber': () => import('@/views/components/minOrMaxNumber.vue'),//导出弹窗
    'addUserGroup': () => import('./addUserGroup.vue'),//导出弹窗
    'addRules': () => import('./addRules.vue'),//导出弹窗
    'addRecord': () => import('./addRecord.vue'),//导出弹窗
  },
  data() {
    return {
      queryParams: {},
      timeRange: [],
      showAddUserGroup:false,
      showAddRules:false,
      showAddRecord:false,
      dataList:[
        {value:1000,name:'新增群成员数',},
        {value:1200,name:'新退群成员数',},
        {value:1200,name:'新增群聊数',},
        {value:600,name:'群聊总数',},
        {value:800,name:'群成员总数',},
      ],
      sexList:[
        {value:1000,name:'男',},
        {value:1200,name:'女',},
      ],
      lableOpt: [
        {
          value: 1, label: '客户等级', children: [
            {value: 3, label: '核心',},
            {value: 4, label: '重要',},
            {value: 5, label: '一般',}
          ]
        },
        {
          value: 2, label: '客户等级2', children: [
            {value: 6, label: '可爱',},
            {value: 7, label: '知性',},
            {value: 8, label: '性感',}
          ]
        }
      ],
      lineXDataList:['2023-01', '2023-02', '2023-03', '2023-04'],
      lineYDataList:[1000, 600, 600, 200],
      userList: [
        {
          value: '1',
          label: '员工1'
        }, {
          value: '2',
          label: '员工2'
        }
      ],
      selectedRange: 'today',
      tableHeight:600,
      tableColumns: [
        {key: 'name', label: `客户群`, visible: true, minWidth: 150, fixed: true},
        {key: 'mobile', label: `所属企微号`, visible: true, minWidth: 150, fixed: false},
        {key: 'email', label: `群成员总数`, visible: true,sortable:true, minWidth: 200,showHeaderTip:'截止选择时段的最后一天时的群成员人数',},
        {key: 'last_login_time', label: `入群人数`, visible: true, sortable:true,  minWidth: 200,showHeaderTip:'选择时段内入群的人数，不去重，一个人多次入群算多次',},
        {key: 'create_time', label: `退群人数`, visible: true, sortable:true,  minWidth: 200,showHeaderTip:'选择时段内退群的人数，不去重，一个人多次退群群算多次',},
        {key: 'check_payCreate_time', label: `发言人数`, visible: true,sortable:true,  minWidth: 200,showHeaderTip:'选择时间段内有发消息的人数，去重，一个人多日都有发消息算一人',},
        {key: 'check_payStatus_name', label: `发言条数 `, visible: true,sortable:true,  minWidth: 150,showHeaderTip:'时间段内发言消息数总和',},
        {key: 'check_payStatus_name', label: `活跃度 `, visible: true, sortable:true, minWidth: 150,showHeaderTip:'选择时段内发言人数/选择时段最后一天群成员总数x100%。',},
        {key: 'create_time', label: `加入监测时间 `, visible: true,sortable:true,  minWidth: 150,showHeaderTip:'最近一次添加监测的时间',},
        {key: 'create_time', label: `添加者 `, visible: true,sortable:true,  minWidth: 150,showHeaderTip:'操作添加该群的账号',},
      ],
      tableDataList: [],
      total: 8, // 表格的总条数
    }
  },
  watch: {},
  computed: {
    dateRange() {
      const now = new Date();
      switch (this.selectedRange) {
        case 'yesterday':
          return [
            new Date(now.getFullYear(), now.getMonth(), now.getDate() - 1),
            new Date(now.getFullYear(), now.getMonth(), now.getDate() - 1),
          ];
        case 'today':
          return [
            now,
            new Date(now.getFullYear(), now.getMonth(), now.getDate()),
          ];
        case 'last7Days':
          return [
            new Date(now.getTime() - (6 * 24 * 60 * 60 * 1000)),
            now,
          ];
        case 'last15Days':
          return [
            new Date(now.getTime() - (14 * 24 * 60 * 60 * 1000)),
            now,
          ];
        case 'last30Days':
          return [
            new Date(now.getTime() - (29 * 24 * 60 * 60 * 1000)),
            now,
          ];
        default:
          return [];
      }
    },
  },
  created() {
    //this.getList();

  },
  methods: {
    handleSortChange({ column, prop, order }) {
      console.log('column',column);
      console.log('prop',prop);
      console.log('order',order);
    },
    getNumber(obj){
      console.log('获取最大值',obj);
    },
    addRecord(){
      this.showAddRecord=true;
    },
    getAddRules(){
      this.showAddRules=true;
    },
    getTime(type) {
      this.selectedRange = type;
      setTimeout(() => {
        console.log('时间范围', this.dateRange);
        this.timeRange = [];
        this.timeRange = [this.fiterTime(this.dateRange[0]), this.fiterTime(this.dateRange[1])];
        console.log('转化时间', this.timeRange);
      }, 0)
    },
    fiterTime(value) {
      if (!value) return '';
      const date = new Date(value);
      const year = date.getFullYear();
      const month = (date.getMonth() + 1).toString().padStart(2, '0');
      const day = date.getDate().toString().padStart(2, '0');
      return `${year}-${month}-${day}`;
    },
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listUser(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.tableDataList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    getAddUserGroup(){
      this.showAddUserGroup=true;
    },
  },
}
</script>


<style scoped lang="scss">
.title {
  display: flex;
  align-items: center;
  padding: 16px 0 0;
  font-weight: bold;
  font-size: 16px;
  margin-bottom:15px;
  position: relative;

  .icon {
    font-size: 12px;
    font-weight: 400;
    color: #3070ff;
    margin-left: 10px;
  }
  .time {
    position: absolute;
    right: 0px;
    top: 10px;
    color: #8C8C8C;
    font-size: 12px;
    font-weight: 400;
  }
}

.timeType {
  display: flex;
  align-items: center;
  margin-right: 10px;

  li {
    border: 1px solid #eee;
    height: 32px;
    margin: 0;
    padding: 0 15px;
    list-style: none;
    cursor: pointer;

    &.active {
      border-color: #2d8cf0;
      color: #2d8cf0;
    }
  }
}

.time-big {
  display: flex;
  align-items: center;
}
.formGroup {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  width: 100%;
}
.dataScreening {
  display: flex;
  padding: 20px;
  background-color: #f7f7f7;
  border-radius: 4px;
  margin-bottom: 20px;
  li {
    flex: 1;
    list-style: none;
    .li-title {
      margin-bottom: 4px;
      color: rgba(0, 0, 0, .45);
      font-size: 14px;
    }
    .li-number {
      color: rgba(0, 0, 0, .85);
      font-size: 24px;
      font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";
    }
  }
}

.center-title {
  height: 30px;
  display: flex;
  align-items: center;
  span {
    content: "";
    width: 4px;
    height: 14px;
    background-color: #3070ff;
    display: block;
    margin-right: 5px;
  }
}
.trendAnalysis {
 height: 350px;
  width: 100%;

}
.tableAnalysis {
  padding-bottom: 20px;
  position: relative;
  .center-title {
    margin-bottom: 20px;
  }
  .exportData {
    position: absolute;
    right: 0px;
    top: 0px;

  }
}
.btn-group {
  display: flex;
  margin-bottom: 10px;

}
.search {
  display: flex;

  ::v-deep.el-input__inner {
    border-right: none !important;
    border-radius: 4px 0px 0px 0px;
  }

  ::v-deep .el-button--default {
    border-radius: 0px 4px 4px 0px;
    padding: 0px 10px;
  }
}
</style>
